#if UNITY_EDITOR

using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine;
using XFGameFramework.ExcelSystem;


namespace XFGameFramework.BuffSystem
{
    public class CustomBuffEffectDataExcel : CustomExcelFieldImportAndExport
    {
        public override object Export(object scriptableObject,object obj, ExcelFieldsType type)
        {
            List<EffectData> effectData = obj as List<EffectData>;

            List<int> ids = new List<int>();
            foreach (var item in effectData)
            {
                if(item == null) continue;
                ids.Add(item.id);
            }

            return JsonConvert.SerializeObject(ids);
        }

        public override object Import(object scriptableObject, object obj, ExcelFieldsType type)
        { 
            return JsonConvert.DeserializeObject<List<int>>(obj.ToString());
        }

        public override void SetValue(object scriptableObject, FieldInfo fieldInfo, object value)
        {  
            List<int> ids = value as List<int>;

            if (ids == null) return;

            BuffConfigData buffConfig = scriptableObject as BuffConfigData;

            if (buffConfig == null) return;

            buffConfig.effects.Clear();

            foreach (var id in ids)
            {
                EffectData effectData = BuffSystemSettings.Instance.GetEffectDataByID(id);
                if(effectData == null) continue; 
                buffConfig.effects.Add(effectData);
            }

            EditorUtility.SetDirty(buffConfig);
        }
    }

}

#endif